<!--
  Copyright (c) 2020 classmate-sun
  [Software Name] is licensed under Mulan PSL v2.
  You can use this software according to the terms and conditions of the Mulan PSL v2.
  You may obtain a copy of Mulan PSL v2 at:
          http://license.coscl.org.cn/MulanPSL2
  THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
  EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
  MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  See the Mulan PSL v2 for more details.
-->

<template>
  <div class="articleShow">
    <section v-for="item in articleList">
      <h5>
        <span>【{{ item.blogType }}】</span>
        <router-link :to="'/Article/' + item.id">{{ item.title }}</router-link>
      </h5>
      <div class="time">
        <p class="date">{{getDay(item.createTime)}}</p>
        <p class="month">
          {{ getMonth(item.createTime) }}
          <span>月</span>
        </p>
        <p class="year">{{getYear(item.createTime)}}</p>
      </div>
      <div class="content">
        <div class="pic">
<!--          <router-link-->
<!--            :to="'/Article/' + item.id"-->
<!--            :style="{ backgroundImage: 'url(' + item.surface + ')' }"-->
<!--          >-->
<!--            <i></i>-->
<!--          </router-link>-->
        </div>
        <div class="text">
          <router-link
            :to="'/Article/' + item.id"
            ><div class="contenttext">
              {{ item.blogHtml | contentToText }}
            </div></router-link
          >
        </div>
      </div>
      <div class="read-more">
        <router-link :to="'/Article/' + item.id">继续阅读</router-link>
      </div>
      <div class="footer">
        <div class="fl">
          <i class="el-icon-s-promotion"></i>
          <span>{{ item.likeNums }}</span>
        </div>
        <div class="fr">
          <span class="pv">
            <i class="el-icon-view"></i>
            <i>{{ item.readNums }}</i>
          </span>
<!--          <span class="comment">-->
<!--            <i class="el-icon-chat-dot-round"></i>-->
<!--            <i>{{ item.comment.length }}</i>-->
<!--          </span>-->
        </div>
      </div>
    </section>
    <div v-if="ifLoding" class="loading">
      <span>加载中</span>
      <svg viewBox="25 25 50 50" class="circular">
        <circle cx="50" cy="50" r="20" fill="none" class="path" />
      </svg>
    </div>
    <p v-if="ifNoMore" class="no-more">哼╭(╯^╰)╮我也是有底线的！！</p>
  </div>
</template>

<script>
// import request from "../api";
//
// const getArticleShow = request.getArticleShow;
import {getYear,getMonth,getDay }from "../assets/js/dateformat"
export default {
  name: "ArticleShow",
  data() {
    return {
      test2:{},
      test:['1','2','3'],
      page:1,
      //文章数据
      articleList: [],
      data:{},
      //no-more的显示与否
      ifNoMore: false,

      //是否在加载
      ifLoding: false,
    };
  },

  filters: {

    contentToText(value) {
      var div = document.createElement("div");
      div.innerHTML = value;
      return div.innerText;
    },
  },

  computed: {
    id() {
      return this.$route.params.id;
    },
  },

  watch: {
    id() {
      //当id发生变化时
      this.getArticleShowFresh();
      document.documentElement.scrollTop = 0;
    },
  },

  methods: {
    getArticleShow( id,boolean){//boolean为false是表面是页面继续增加页数
      if(!boolean){
        this.page++;
        this.$ajax.post(this.$config.url +`/ljblog/blog/getBlogList/${this.page}`,{
          "blogClassify":id,
          "blogStatus":1,
        }).then(res => {
          this.ifLoding = false;
          if(res.data.status===this.$config.request_code_success){
            if (res.data.data.size!==0) {
              this.articleList.push(res.data.data.list);
            } else {
              this.ifNoMore = true;
            }
          }else{
            this.$message.error("服务器出错了，请联系我，感谢")
          }
        });
      }else{
        this.page=1;
        this.$ajax.post(this.$config.url +`/ljblog/blog/getBlogList/${this.page}`,{
          "blogClassify":id,
          "blogStatus":1,
        }).then(res => {
          if(res.data.status===this.$config.request_code_success){
            this.articleList=res.data.data.list

            this.data=res.data;
          }else{
            this.$message.error("服务器出错了，请联系我，感谢")
          }
        });
      }
    },
    getArticleShowFresh() {
      this.ifNoMore = false;
      this.ifLoding = false;
      this.getArticleShow(null, true);
    },

    handleScroll() {
      if (this.ifNoMore || this.ifLoding) return;

      //文档高
      let c = document.documentElement.offsetHeight;
      //滚动高
      let a = document.documentElement.scrollTop;
      //可视区高
      let b = document.documentElement.clientHeight;

      if (a + b >= c - 200) {
        this.ifLoding = true;
        this.getArticleShow(this.id, false);
      }
    },

    getYear(date){
      return getYear(date);
    },
    getMonth(date){
      return getMonth(date);
    },
    getDay(date){
      return getDay(date);
    },

  },

  mounted: function (){
    //发送第一次请求
    this.getArticleShowFresh();

    //监听滚动事件
    window.addEventListener("scroll", this.handleScroll);
  },
};
</script>

<style scoped lang="less">
.articleShow {
  > section {
    overflow: hidden;
    position: relative;
    box-sizing: border-box;
    width: 100%;
    height: 369px;
    padding: 20px 30px 25px;
    margin-bottom: 20px;
    background-color: rgba(255, 255, 255, 0.8);
    animation: show 0.5s 1;
    animation-fill-mode: forwards;
    border-radius: 15px;
    @keyframes show {
      from {
        opacity: 0.5;
        transform: scale(0.5);
      }
      to {
        opacity: 1;
        transform: scale(1);
      }
    }
    &:nth-child(1)::before {
      content: "最新";
      position: absolute;
      height: 20px;
      line-height: 20px;
      text-align: center;
      width: 74px;
      background-color: #ff5722;
      color: #fff;
      transform: rotate(-45deg);
      left: -18px;
      top: 9px;
    }
    > h5 {
      line-height: 30px;
      padding: 5px 130px 5px 0;
      border-bottom: 1px solid #e8e9e7;
      font-size: 18px;
      font-weight: 400;
      span {
        font-size: 16px;
        font-weight: 400;
        display: inline-block;
        vertical-align: bottom;
        color: #2ea7e0;
      }
      a {
        color: #000;
        text-decoration: none;
        &:hover {
          color: #6bc30d;
          text-decoration: underline;
        }
      }
    }
    .time {
      width: 100px;
      height: 70px;
      font-family: SourceCodeProRegular, Menlo, Monaco, Consolas, "Courier New",
        monospace, "Helvetica Neue", Arial, sans-serif;
      position: absolute;
      right: 10px;
      top: 10px;
      background-color: rgba(0, 0, 0, 0);
      padding: 0 20px 5px 20px;
      line-height: 32px;
      .date {
        display: block;
        text-align: center;
        font-weight: 700;
        font-size: 40px;
        color: #6bc30d;
        position: relative;
        top: 2px;
      }
      .month {
        display: inline-block;
        color: #989997;
        font-size: 18px;
        span {
          font-size: 14px;
        }
      }
      .year {
        display: inline-block;
        color: #989997;
        margin-left: 10px;
        font-size: 18px;
      }
    }
    .content {
      margin-top: 20px;
      line-height: 28px;
      position: relative;
      min-height: 200px;
      > .pic a {
        position: relative;
        display: block;
        overflow: hidden;
        float: left;
        width: 300px;
        height: 180px;
        border: 1px solid #e8e9e7;
        margin-right: 20px;
        background-position: center top;
        background-size: cover;
        i {
          position: absolute;
          top: 0;
          left: 0;
          display: block;
          width: 0;
          height: 100%;
          /*background-color: #fff;*/
          transform: translateX(-40px) skew(-15deg);
          box-shadow: 0 0 30px 20px rgba(255, 255, 255, 0.2);
        }
        &:hover i {
          transition: transform 0.5s;
          transform: translateX(350px) skew(-15deg);
        }
      }
      > .text {
        overflow: hidden;
        display: block;
        height: 195px;
      }
    }
    .read-more {
      position: relative;
      &::before {
        content: " ";
        display: inline-block;
        height: 1px;
        top: 20px;
        left: 100px;
        right: 0;
        position: absolute;
        background-color: #d0d0d0;
      }
      a {
        font-weight: bold;
        color: #383937;
        text-decoration: none;
        border: none;
        line-height: 40px;
        &:hover {
          color: #6bc30d;
          text-decoration: underline;
        }
      }
    }
    .footer {
      .fl {
        float: left;
        i {
          font-size: 18px;
          margin-right: 5px;
          color: #666;
        }
        span {
          display: inline-block;
          font-size: 12px;
          padding: 2px 5px;
          background-color: #f1f2f0;
          color: #787977;
          margin: 2px;
          text-decoration: none;
          -webkit-transition: all 0.2s;
          transition: all 0.2s;
          &:hover {
            color: #fff;
            background: #6bc30d;
          }
        }
      }
      .fr {
        float: right;
        color: #666;
        i {
          font-style: normal;
          margin-right: 12px;
        }
        .comment {
          margin-left: 20px;
        }
      }
    }
  }
  > .loading {
    position: relative;
    height: 42px;
    background-color: rgba(255, 255, 255, 0.8);
    text-align: center;
    border-radius: 15px;
    span {
      display: inline-block;
      height: 42px;
      line-height: 42px;
      vertical-align: middle;
    }
    svg {
      vertical-align: middle;
      margin-left: 5px;
      height: 18px;
      width: 18px;
      animation: loading-rotate 2s linear infinite;
      circle {
        animation: loading-dash 1.5s ease-in-out infinite;
        stroke-dasharray: 90, 150;
        stroke-dashoffset: 0;
        stroke-width: 2;
        stroke: #409eff;
        stroke-linecap: round;
        color: black;
      }
    }
    @keyframes loading-dash {
      0% {
        stroke-dasharray: 1, 200;
        stroke-dashoffset: 0;
      }
      50% {
        stroke-dasharray: 90, 150;
        stroke-dashoffset: -40px;
      }
      to {
        stroke-dasharray: 90, 150;
        stroke-dashoffset: -120px;
      }
    }
    @keyframes loading-rotate {
      to {
        transform: rotate(1turn);
      }
    }
  }
  > .no-more {
    height: 30px;
    line-height: 30px;
    text-align: center;
    background-color: rgba(255, 255, 255, 0.8);
    font-size: 12px;
    border-radius: 15px;
  }
}
</style>
